/**
 * 
 * Copyright (C) 2011 Cody Stoutenburg . All rights reserved.
 *
 *       This program is free software; you can redistribute it and/or
 *       modify it under the terms of the GNU Lesser General Public License
 *       as published by the Free Software Foundation; either version 2.1
 *       of the License, or (at your option) any later version.
 *
 *       This program is distributed in the hope that it will be useful,
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *       GNU Lesser General Public License for more details.
 *
 *       You should have received a copy of the GNU Lesser General Public License
 *       along with this program; if not, write to the Free Software
 *       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 * 
 */
package ca.usherbrooke.goal;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Observable;

import ca.usherbrooke.model.world.IWorldModel;

/**
 * @author Cody Stoutenburg
 * 
 */
public class EntityGoal extends Observable implements Serializable {
	private static final long serialVersionUID = 1L;

	private final Long guid;
	private Float score;

	private final HashMap<Long, BasicGoal> allGoals;
	private final HashMap<Long, BasicGoal> allFinish;

	public EntityGoal(Long guid) {
		this.guid = guid;
		this.score = 0f;

		allGoals = new HashMap<Long, BasicGoal>();
		allFinish = new HashMap<Long, BasicGoal>();
	}

	public Long getEntityGuid() {
		return this.guid;
	}

	public Collection<BasicGoal> getAllGoals() {
		return allGoals.values();
	}

	public Collection<BasicGoal> getAllGoalsSolved() {
		return allFinish.values();
	}

	/**
	 * 
	 * @param guid
	 * @return the goal, or null if no goal found with this id
	 */
	public BasicGoal getFinishGoal(Long guid) {
		return this.allFinish.get(guid);
	}

	/**
	 * 
	 * @param guid
	 * @return the goal, or null if no goal found with this id
	 */
	public BasicGoal getGoal(Long guid) {
		return this.allGoals.get(guid);
	}

	public Boolean isGoalSolve(Long guid) {
		return this.allFinish.get(guid) != null;
	}

	public void addGoal(BasicGoal goal) {
		if (goal.getEntity().equals(guid)) {
			this.allGoals.put(goal.getGuid(), goal);
			this.setChanged();
			this.notifyObservers();
		}
	}

	public Boolean removeGoal(BasicGoal goal) {
		return allGoals.remove(goal.getGuid()) != null;
	}

	public void updateGoals(IWorldModel world, Long update) {
		Boolean haveGoalFinish = false;
		Boolean remainingChange = false;

		Float begin = score;
		for (BasicGoal goal : allGoals.values()) {
			Long before = goal.getRemainingTime();
			goal.update(world, update);

			remainingChange = !before.equals(goal.getRemainingTime());

			// update score at each update
			score += goal.getCurrentScore();

			if (goal.isTerminated()) {
				haveGoalFinish = true;
				finishGoal(goal);
			}
		}
		if (!begin.equals(score) || haveGoalFinish || remainingChange) {
			this.setChanged();
			this.notifyObservers();
		}
	}

	private void finishGoal(BasicGoal goal) {
		allGoals.remove(goal.getGuid());
		allFinish.put(goal.getGuid(), goal);
	}

	public Float getScore() {
		return this.score;
	}
}
